/**
 * Created by Flower付成华 on 2018/5/15.
 */

var playstate = 0;//默认的播放类型， 0：单句，1：本课  2：复读 3：连续 4：查词  
var audioHandler ;
var strs = [];
var curpindex = 0;
var curpagenum = 0;
var curobject;
var ispause = 0;//是否暂停
var curtime;//当前的 settimeout
var istranslation = false;
var curalltime = 5;//默认音频的播放时间
var curtrans = "";//当前选中的解释
var checkplay = false;//判断当前是否为点击播放音频，还是自动播放音频

var page_divs = new Array();//所有页面的div对象几个，二维数组
var divs = new Array();//一个页面的div 集合对象

var cur_volume = 50;//当前音量是多少
var isshowvolume = false;//是否显示开启音量

var isshowchecked = false;//是否显示可点击选读框


var isreadingover = false;//判断是否已经读取完了

//录音相关
var isfollow = false;//当前是否开启了跟读模式
var record_playnum = 1;//用来判断当前课本是第几次播放，来控制播放本地录音

//key都为小写字母
var dicdata = {
    'classroom':'教室',
    'window':'窗户',
    'blackboard':'黑板',
    'light':'电灯',
    'picture':'图画',
    'door':'门',
    'teacher’s desk':'讲台',
    'computer':'计算机',
    'my':'我的',
    'schoolbag':'书包',
    'clean':'打扫',
    'desk':'书桌',
    'chair':'椅子',
    'new':'新的',
    'fish':'鱼，鱼肉',
    'bow':'碗',
    'fish bow':'鱼缸',
    'really':'（表示兴趣或惊讶）真的',
    'big':'大的',
    'look':'看',
    'near':'距离近',
    'tv':'电视',
    'fan':'风扇',
    'wall':'墙壁',
    'floor':'地板'
};

var fudu_content = new Array();//要复读的内容 
var start_du_page;//开始从几页开始复读
var end_du_page;//结束从几页开始复读
var isfuduing = false;//判断是否正在复读
var pageindex = 0;
var nextpageindex_add = 1;//下页面的index 叠加量
var swiper;//初始化swiper对象
var cur_play_sudu = 1;//当前播放的音频速率
var position_data = [
{
    page_data:[]
},
{
    page_data:[
    {
        text:'Unit 1 My classroom',
        translation:'第一单元 我的教室',
        duration:3,
        x:20.5,
        y:11.1,
        w:45.1,
        h:5.9
    },
    {
        text:'Unit 2 My schoolbag',
        translation:'第二单元 我的书包',
        duration:3,
        x:20.5,
        y:18.1,
        w:45.1,
        h:5.9,
        Duplicate:true
    },
    {
        text:'Unit 3 My friends',
        translation:'null',
        duration:3,
        x:20.5,
        y:24.5,
        w:40.2,
        h:5.9,
        Duplicate:false
    },
    {
        text:'null',
        translation:'null',
        duration:0.0,
        x:60.5,
        y:31.0,
        w:30.3,
        h:6
    },
    {
        text:'Recycle 1',
        translation:'复习一',
        duration:2,
        x:20.5,
        y:31.0,
        w:24.3,
        h:5.9
    },
    {
        text:'Unit 4 My home',
        translation:'第四单元 我的家',
        duration:3,
        x:20.5,
        y:37.8,
        w:34.3,
        h:5.9
    },
    {
        text:'Unit 5 Dinner\'s ready',
        translation:'第五单元 晚餐准备好了,第五单元 晚餐准备好了,第五单元 晚餐准备好了第五单元 晚餐准备好了,第五单元 晚餐准备好了,第五单元 晚餐准备好了第五单元 晚餐准备好了,第五单元 晚餐准备好了,第五单元 晚餐准备好了第五单元 晚餐准备好了,第五单元 晚餐准备好了,第五单元 晚餐准备好了第五单元 晚餐准备好了,第五单元 晚餐准备好了,第五单元 晚餐准备好了第五单元 晚餐准备好了,第五单元 晚餐准备好了,第五单元 晚餐准备好了第五单元 晚餐准备好了,第五单元 晚餐准备好了,第五单元 晚餐准备好了第五单元 晚餐准备好了,第五单元 晚餐准备好了,第五单元 晚餐准备好了第五单元 晚餐准备好了,第五单元 晚餐准备好了,第五单元 晚餐准备好了第五单元 晚餐准备好了,第五单元 晚餐准备好了,第五单元 晚餐准备好了',
        duration:3,
        x:20.5,
        y:45.1,
        w:48.8,
        h:5.9
    },
    {
        text:'Unit 6 Meet my family!',
        translation:'第六单元 认识一下我的家庭成员',
        duration:4,
        x:20.5,
        y:52.3,
        w:52.2,
        h:5.9
    },
    {
        text:'Recycle 2',
        translation:'复习二',
        duration:2,
        x:20.5,
        y:59.1,
        w:24.3,
        h:5.9
    },
    {
        text:'Words in each unit',
        translation:'附录一 单元词汇表',
        duration:3,
        x:20.5,
        y:65.7,
        w:43.3,
        h:5.9
    },
    {
        text:'Vocabulary',
        translation:'附录二 词汇表',
        duration:3,
        x:20.5,
        y:72.8,
        w:24.3,
        h:5.9
    },
    {
        text:'Useful expressions',
        translation:'附录三 常用表达法',
        duration:3,
        x:20.5,
        y:79.6,
        w:43.3,
        h:5.9,
        Duplicate:true
    }
    ]
}
,
{
    page_data:[
    {
        text:'ZhangPeng',
        translation:'张鹏',
        duration:2,
        w:266,
        h:267,
        x:123,
        y:170
    },
    {
        text:'Sarah',
        translation:'萨拉',
        duration:2,
        w:266,
        h:267,
        x:404,
        y:170
    },
    {
        text:'Miss White',
        translation:'怀特小姐',
        duration:2,
        x:692,
        y:169,
        w:266,
        h:267
    },
    {
        text:'Wuyifan',
        translation:'吴一凡',
        duration:2,
        x:123,
        y:462,
        w:266,
        h:267
    },
    {
        text:'ChenJie',
        translation:'陈杰',
        duration:2,
        x:404,
        y:462,
        w:266,
        h:267
    },
    {
        text:'Mike',
        translation:'麦克',
        duration:2,
        x:692,
        y:462,
        w:266,
        h:267
    },
    {
        text:'MrJones',
        translation:'琼斯先生',
        duration:2,
        x:122,
        y:759,
        w:254,
        h:249
    },
    {
        text:'John',
        translation:'约翰',
        duration:2,
        x:414,
        y:759,
        w:254,
        h:249
    },
    {
        text:'Amy',
        translation:'埃米',
        duration:2,
        x:706,
        y:764,
        w:254,
        h:249
    },
    {
        text:'Zoom',
        translation:'佐姆',
        duration:2,
        x:222,
        y:1009,
        w:263,
        h:171
    },
    {
        text:'Zip',
        translation:'齐普',
        duration:2,
        x:629,
        y:1080,
        w:263,
        h:171
    }
    ]
},
{
    page_data:[
    {
        text:'Unit One',
        translation:'第一单元',
        duration:3,
        x:296,
        y:137,
        w:344,
        h:84
    },
    {
        text:'Where\'s my schoolbag?',
        translation:'我的书包在哪里？',
        duration:3,
        x:265,
        y:380,
        w:323,
        h:76
    },
    {
        text:'It\'s near the computer.',
        translation:'就在电脑旁边。',
        duration:3,
        x:552,
        y:560,
        w:328,
        h:75
    },
    {
        text:'Let\'s clean the desks and chairs.',
        translation:'我们擦擦书桌和椅子吧。',
        duration:3,
        x:222,
        y:633,
        w:263,
        h:108
    },
    {
        text:'Let me clean the windows.',
        translation:'我来擦窗户。',
        duration:3,
        x:260,
        y:1016,
        w:357,
        h:76,
        Duplicate:true
    }

    ]
},
{
    page_data:[
    {
        text:'My classroom',
        translation:'我的教室',
        duration:3,
        x:246,
        y:141,
        w:491,
        h:76
    },
    {
        text:'Hey,ZhangPeng We have a new classroom.',
        translation:'你好张鹏，我们有一间新教室。',
        duration:4,
        x:575,
        y:226,
        w:315,
        h:112
    },
    {
        text:'Let me clean the fish bowl.',
        translation:'我来擦鱼缸。',
        duration:3,
        x:192,
        y:511,
        w:363,
        h:78
    },
    {
        text:'Excuse me.',
        translation:'劳驾！',
        duration:3,
        x:460,
        y:747,
        w:183,
        h:73
    },
    {
        text:'Oh, sorry.',
        translation:'噢，对不起。',
        duration:3,
        x:601,
        y:913,
        w:184,
        h:74,
        Duplicate:false
    }

    ]
},
{
    page_data:[
    {
        text:'Let\'s talk',
        translation:'A 让我们来学对话吧。',
        duration:3,
        x:177,
        y:129,
        w:297,
        h:53
    },
    {
        text:'Hey,ZhangPeng.We have a new classroom.',
        translation:'你好张鹏，我们有一间新教室。',
        duration:3,
        x:92,
        y:227,
        w:359,
        h:109
    },
    {
        text:'Really? What\'s in the classroom?',
        translation:'真的？教室里有什么？',
        duration:3,
        x:176,
        y:340,
        w:298,
        h:100
    },
    {
        text:'Let\'s go and see!',
        translation:'我们去看看吧。',
        duration:3,
        x:82,
        y:445,
        w:267,
        h:84
    },
    {
        text:'It\'s so big!',
        translation:'真大呀！',
        duration:3,
        x:536,
        y:214,
        w:197,
        h:78
    },
    {
        text:'Look! My picture!',
        translation:'看！我的画！',
        duration:3,
        x:614,
        y:296,
        w:262,
        h:73
    },
    {
        text:'Where is it?',
        translation:'它在哪儿？',
        duration:3,
        x:537,
        y:371,
        w:206,
        h:78
    },
    {
        text:'It\'s near the window.',
        translation:'就在窗户旁边。',
        duration:3,
        x:580,
        y:458,
        w:298,
        h:74
    },
    {
        text:'Let\'s play',
        translation:'让我们来玩一玩吧',
        duration:3,
        x:177,
        y:771,
        w:275,
        h:65
    },
    {
        text:'I see a "c".',
        translation:'我看见一个“c”。',
        duration:3,
        x:135,
        y:841,
        w:184,
        h:88
    },
    {
        text:'Where is it?',
        translation:'它在哪儿？',
        duration:3,
        x:337,
        y:901,
        w:206,
        h:84
    },
    {
        text:'It\'s in a desk.',
        translation:'它在书桌里。',
        duration:3,
        x:120,
        y:956,
        w:204,
        h:85
    },
    {
        text:'It\'s a crayon.',
        translation:'它是支蜡笔。',
        duration:3,
        x:339,
        y:1180,
        w:215,
        h:82
    }

    ]
}
,
{
    page_data:[
    {
        text:'Let\'s learn',
        translation:'让我们来学一学吧。',
        duration:3,
        x:218,
        y:127,
        w:328,
        h:55
    },
    {
        text:'classroom',
        translation:'教室',
        duration:3,
        x:137,
        y:286,
        w:138,
        h:33
    },
    {
        text:'window',
        translation:'窗户',
        duration:3,
        x:231,
        y:492,
        w:109,
        h:34
    },
    {
        text:'blackboard',
        translation:'黑板',
        duration:3,
        x:390,
        y:295,
        w:159,
        h:35
    },
    {
        text:'light',
        translation:'电灯',
        duration:3,
        x:638,
        y:135,
        w:83,
        h:34
    },
    {
        text:'pricture',
        translation:'图画',
        duration:3,
        x:768,
        y:123,
        w:111,
        h:31
    },
    {
        text:'door',
        translation:'门',
        duration:3,
        x:852,
        y:240,
        w:87,
        h:48
    },
    {
        text:'What\'s in the classroom?',
        translation:'教室里有什么？',
        duration:3,
        x:375,
        y:459,
        w:363,
        h:87
    },
    {
        text:'One blackboard,on TV,many desks and chairs...',
        translation:'一块黑板，一台电视机，许多书桌和椅子。',
        duration:5,
        x:503,
        y:635,
        w:369,
        h:128
    },
    {
        text:'Let\'s do',
        translation:'让我们来做一做吧',
        duration:3,
        x:227,
        y:775,
        w:254,
        h:69
    },
    {
        text:'Open the door.',
        translation:'开门',
        duration:3,
        x:188,
        y:880,
        w:213,
        h:54
    },
    {
        text:'Turn on the light',
        translation:'开灯',
        duration:3,
        x:533,
        y:959,
        w:238,
        h:53
    },
    {
        text:'Close the window.',
        translation:'关窗',
        duration:3,
        x:177,
        y:1040,
        w:250,
        h:57
    },
    {
        text:'Put up the picture.',
        translation:'贴图画',
        duration:3,
        x:519,
        y:1127,
        w:259,
        h:52
    },
    {
        text:'Clean the blackboard.',
        translation:'檫黑板',
        duration:3,
        x:180,
        y:1211,
        w:294,
        h:53,
        Duplicate:true
    }

    ]
}
];

//菜单的选择事件
function rightmenu_click(onthis){
    var topage = $$(onthis).attr("curpage");
    dismissRightMenu();
    if(isNotNull(swiper)){
        var mySwiper = document.querySelector('.swiper-container').swiper;
        mySwiper.slideTo(topage,1000);
    }
    
}


//判断手机类型
window.onload = function () {

    // 跳转本地页面、前面要加上local关键字

    $$(".swiper-wrapper").html('<div class="swiper-slide"><img src="../img_test/en_book_0.png" alt="" ></div><div class="swiper-slide"><img src="../img_test/en_book_1.png" alt="" ></div><div class="swiper-slide"><img src="../img_test/en_book_2.png" alt="" ></div></div><div class="swiper-slide"><img src="../img_test/en_book_3.png" alt="" ></div><div class="swiper-slide"><img src="../img_test/en_book_4.png" alt="" ></div><div class="swiper-slide"><img src="../img_test/en_book_5.png" alt="" ></div><div class="swiper-slide"><img src="../img_test/en_book_6.png" alt="" ></div>');


    //初始化audio
    initAudio();

    //执行默认
    ispause = 1;
    audioHandler.pause();
    $("#bottom_menu_img2").attr("src","../img/icon_play.png");


    clearBorder();
    // $("#div_container div").css('border','0px solid white');

    //动态创建btn
    createCheckImage(0);

   //banner必备
   swiper = new Swiper('.swiper-container', {
      spaceBetween:30,
      followFinger:false,
      touchAngle:50,
      touchMoveStopPropagation:false,
      centeredSlides:true,
      touchRatio:1,
      speed:300,
      // effect : 'cube',
      // cubeEffect: {
      //     slideShadows: true,
      //     shadow: true,
      //     shadowOffset: 100,
      //     shadowScale: 0.7
      //   },
      // effect : 'fade',
      //   fadeEffect: {
      //     crossFade: false,
      //   },

      on: {
              // slideNextTransitionStart: function () {//下一页
              //   var index = swiper.activeIndex;
              //   console.log("slideNextTransitionStart"+index);
              //   // alert(index);
              // },
              // slidePrevTransitionStart:function (){// 上一页
              //   var index = swiper.activeIndex;
              //   console.log("slidePrevTransitionStart"+index);
              //   // alert(index);
              // },
              touchStart: function(swiper,even){
                console.log('bg____touchStart;');

                    // var endx, endy;
                    // endx = even.changedTouches[0].pageX;
                    // endy = even.changedTouches[0].pageY;
                    // var direction = getDirectionBegin(startx, starty, endx, endy);
                    // $(".swiper-container").css("zIndex",99);

                    var event = event || window.event;
                    starty = event.touches[0].pageY;
                },
                touchMove: function(swiper,even){
                // console.log('bg____touchMove;');

                var event = event || window.event;
                event.touches[0].pageY;

                var endx, endy;
                endx = event.changedTouches[0].pageX;
                endy = event.changedTouches[0].pageY;

                var direction = getDirectionBegin(startx, starty, endx, endy);
                
                
            },
            touchEnd: function (swiper,even){
                dismissVolume();
            },
            click: function(swiper,even){
                console.log('click;');
                if(pageindex == 0){
                        var mySwiper = document.querySelector('.swiper-container').swiper;
                        mySwiper.slideTo(1,1000);                                        
                }
            },
              slideChangeTransitionStart:function(){//先执行start

                pageindex = swiper.activeIndex;
                if(playstate == 2){//圈选模式
                    // alert("start_du_page:"+start_du_page +"end_du:"+end_du_page+"==="+index);
                    if(start_du_page != pageindex){
                        clearBorder();
                        // $("#div_container div").css('border','0px solid white');
                    }

                    if(isfuduing){//正在读
                        if(start_du_page == pageindex){
                            clearBorder();
                            // $("#div_container div").css('border','0px solid white');
                            $(fudu_content[0]).css('border','1px solid #0665A5');
                            
                        }
                        if(end_du_page == pageindex){
                            clearBorder();
                            // $("#div_container div").css('border','0px solid white');
                            $(fudu_content[1]).css('border','1px solid red');

                        }
                    }else{
                        //动态创建btn
                        createCheckImage(pageindex);    
                    }

                    
                    return;
                }
                //执行默认
                dismissRead();
                clearBorder();
                // $("#div_container div").css('border','0px solid white');
                // clearBorder();
                // $("#div_container div").css('border','0px solid white');
                //动态创建btn
                createCheckImage(pageindex);
                
            }
        }

    });
   
}


//改变播放音量
function changeVolume(num){
    $("#sound_set_div").show();
    console.log("改变："+num);
    // $("#sound_set_div_progress").height(cur_volume+"%");
    $("#sound_set_div_progress").height(Math.round(cur_volume)+"%");
    cur_volume = Number(cur_volume) - Number(num/70);
    console.log("改变音量："+Math.round(cur_volume));
    if(cur_volume > 100){
        cur_volume = 100;
    }
    if(cur_volume < 0){
        cur_volume = 0;
    }
    
    $("#sound_set_div_progress").height(Math.round(cur_volume)+"%");
    $("#v_progress").html("音量"+Math.round(cur_volume)+"%");
    audioHandler.audio.volume = Math.round(cur_volume)/100;
    

}

//关闭音量弹窗
function dismissVolume(){
    $("#sound_set_div").hide();;
    isshowvolume = false;    
}

function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "";
    var seperator2 = "";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
    + "" + date.getHours() + seperator2 + date.getMinutes()
    + seperator2 + date.getSeconds();
    return currentdate;
}


var startx, starty;

    //获得角度
    function getAngle(angx, angy) {
        return Math.atan2(angy, angx) * 180 / Math.PI;
    };

    //根据起点终点返回方向 1向上 2向下 3向左 4向右 0未滑动
    function getDirection(startx, starty, endx, endy) {
        var angx = endx - startx;
        var angy = endy - starty;

        //如果滑动距离太短
        // if (Math.abs(angx) < 2 && Math.abs(angy) < 2) {
        //     return result;
        // }

        if(angx <= -5){//向左边滑动
            return 0;
        }else if(angx >5){//向右边滑动
            return 1;
        }else{
            return 2;
        }
    }

    //根据起点终点返回方向 1向上 2向下 3向左 4向右 0未滑动
    function getDirectionBegin(startx, starty, endx, endy) {
        var angx = endx - startx;
        var angy = endy - starty;
        //如果滑动距离太短
        // if (Math.abs(angx) < 2 && Math.abs(angy) < 2) {
        //     return result;
        // }
        console.log("isshowvolume:   "+isshowvolume+"  endy:"+endy+"   starty"+starty+"   angy:"+angy);

        if(isshowvolume){
            if(angy >= 1){//向下滑动
                console.log("angy"+angy);
                changeVolume(angy);
            }else if(angy <= -1){//向上滑动
                changeVolume(angy);
            }else{

            }
        }

        if(angy >= 50){//开始调控音量
            isshowvolume = true;    
        }else if(angy < -50){
            isshowvolume = true;    
        }
        
    }

    function createCheckImage(pindex){
        console.log(pindex);
        if(position_data.length>0){
            for(var i = 0;i<position_data.length;i++){
                var pages = position_data[i].page_data;
                if(pindex == i){
                    if(pages.length>0){

                        var imgContainer=document.getElementById("div_container");
                    // $("#div_container").html("");
                    //每次都初始化一次
                    divs = new Array();
                    for (var j = 0; j<pages.length; j++){
                        console.log("-----------------pages.length:"+pages.length);
                        var x = pages[j].x;
                        var y = pages[j].y;
                        var w = pages[j].w;
                        var h = pages[j].h;
                        var duration = pages[j].duration;
                        var translationtips = pages[j].translation;
                        var curtext = pages[j].text;
                        var duplicate = pages[j].Duplicate;
                        var cid;
                        var div;
                        if(pindex==1){
                            imgContainer.style.height=100+"%";
                            div = document.createElement("div");
                            var aa = Number(i);
                            var bb = Number(j)+1;
                            cid = aa+"_"+bb;
                            div.id =cid;
                            div.setAttribute("duration",Number(duration));
                            div.setAttribute("cid",aa+"_"+bb);
                            div.setAttribute("curpage",aa+"");
                            div.setAttribute("pagenum",pages.length+"");
                            if(isNotNull(duplicate) && duplicate){
                                div.setAttribute("duplicate",1)
                            }else{
                                div.setAttribute("duplicate",0)
                            }
                            
                            div.backgroundColor = "white";
                            div.title = translationtips+"0_0!!"+curtext;
                            div.style.width = w+"%";
                            div.style.height = h+"%";
                            if(isshowchecked){
                            div.style.border = "1px solid #bababa";
                            }else{
                            div.style.border = "0px solid #0665A5";    
                            }
                            
                            div.style.borderRadius = 5+'px';
                                    // div.style.position="absolute";
                                    div.style.zIndex = "100";
                                    div.style.left=x+"%";
                                    // div.style.zIndex = "100";
                                    div.style.position = "absolute";
                                    div.style.top=y+"%";
                                    // div.style.backgroundColor="green";
                                    // imgContainer.appendChild(div);
                                    addDivChildByNotExist(imgContainer,div,cid);
                                }else{
                                    imgContainer.style.height=100+"%";
                                    div = document.createElement("div");
                                    var aa = Number(i);
                                    var bb = Number(j)+1;
                                    cid = aa+"_"+bb;
                                    div.id =cid;
                                    div.backgroundColor = "white";
                                    div.setAttribute("duration",Number(duration));
                                    div.setAttribute("cid",aa+"_"+bb);
                                    div.setAttribute("curpage",aa+"");
                                    div.setAttribute("pagenum",pages.length+"");
                                    if(isNotNull(duplicate) && duplicate){
                                        div.setAttribute("duplicate",1)
                                    }else{
                                        div.setAttribute("duplicate",0)
                                    }
                                    div.title = translationtips+"0_0!!"+curtext;
                                    div.style.width = (w/1024)*100+"%";
                                    div.style.height = (h/1431)*100+"%";

                                    if(isshowchecked){
                                    div.style.border = "1px solid #bababa";
                                    }else{
                                    div.style.border = "0px solid #0665A5";    
                                    }
                                    
                                    // div.style.position="absolute";
                                    div.style.zIndex = "100";
                                    div.style.borderRadius = 5+'px';
                                    div.style.left=(x/1024)*100+"%";
                                    // div.style.zIndex = "100";
                                    div.style.position = "absolute";
                                    // div.style.top=(y/(1431-(1431/10)))*100+"%";
                                    div.style.top=(y/(1431))*100+"%";
                                    // div.style.backgroundColor="green";
                                    // imgContainer.appendChild(div);
                                    addDivChildByNotExist(imgContainer,div,cid);
                                    console.log(div.style.width);
                                }
                                // console.log($("#div_container").html());

                                div.addEventListener('touchstart',function(){
                                    var event = event || window.event;
                                    switch(event.type){
                                        case "touchstart":
                                        startx = event.touches[0].pageX;
                                        starty = event.touches[0].pageY;
                                        event.touches[0].pageY;
                                        break;

                                    }
                                    
                                }, false);


                                div.addEventListener('touchend',function(){
                                    var event = event || window.event;
                                    switch(event.type){
                                        case "touchstart":
                                                    // console.log("touchstart");
                                                    break;
                                                    case "touchend":
                                                    dismissVolume();
                                                    var endx, endy;
                                                    endx = event.changedTouches[0].pageX;
                                                    endy = event.changedTouches[0].pageY;
                                                    var direction = getDirection(startx, starty, endx, endy);
                                                    // alert(direction);
                                                    switch (direction) {

                                                        case 0://左边
                                                        var mySwiper = document.querySelector('.swiper-container').swiper;
                                                        mySwiper.slideNext(500);
                                                        break;
                                                        case 1://右边

                                                        var mySwiper = document.querySelector('.swiper-container').swiper;
                                                        mySwiper.slidePrev(500);
                                                        break;
                                                            case 2://不滑动
                                                            break;
                                                            default:
                                                        }
                                                        break;
                                                        case "touchmove":
                                                // event.preventDefault();//取消事件的默认动作
                                                console.log("touchmove");
                                                // $(".swiper-container").css("zIndex",102);
                                                break;
                                            }

                                        }, false);


                                div.addEventListener('touchmove',function(){
                                    var event = event || window.event;
                                    switch(event.type){
                                            case "touchmove":
                                            var event = event || window.event;
                                            event.touches[0].pageY;

                                            var endx, endy;
                                            endx = event.changedTouches[0].pageX;
                                            endy = event.changedTouches[0].pageY;

                                            var direction = getDirectionBegin(startx, starty, endx, endy);

                                            break;
                                            }

                                        }, false);

                                // div.addEventListener("click", function (event){

                                //     alert("dd");
                                // },true);
                                imgContainer.onclick = function(event){
                                    if(pindex == 0){
                                        var mySwiper = document.querySelector('.swiper-container').swiper;
                                        mySwiper.slideTo(1,1000);                                        
                                    }
                                }

                                div.onclick=function(event){

                                    checkplay = true;

                                    //判断如果不是本页面的创建的div，不能点击
                                    var this_div_pageindex = $(this).attr("curpage");
                                    // alert("this_div_pageindex----"+this_div_pageindex+"pageindex:"+pageindex);
                                    // alert(pageindex+"--"+this_div_pageindex);
                                    if(pageindex != this_div_pageindex){
                                        return;
                                    }

                                    nextpageindex_add = 1;//每次点击，叠加量归为1

                                    if(playstate == 4){//查词功能，点击弹出内容弹窗，选择复制查词
                                        searchWords(this);
                                        return;
                                    }

                                    if(playstate == 2){//圈选 复读模式
                                        if(fudu_content.length == 0){//还没有圈选
                                            start_du_page = Number(i);
                                            $(this).css('border','1px solid #0665A5');
                                            fudu_content[0] = this;
                                            $.toast("请再选择一个复读终点");
                                        }else{//开始复读
                                            fudu_content[1] = this;

                                            var a = ($(fudu_content[0]).attr("id")+"");
                                            var b = ($(fudu_content[1]).attr("id")+"");
                                            // alert(s_id_a+"="+s_id_b+"="+e_id_a+"="+e_id_b);
                                            //防止开始的地点大于结束的地点
                                            if(compareId(a,b) == 1){//a 比较大

                                                end_du_page = start_du_page;
                                                start_du_page = Number(i);
                                                fudu_content[1] = fudu_content[0];
                                                fudu_content[0] = this;

                                                $(fudu_content[0]).css('border','1px solid #0665A5');

                                            }else{
                                                end_du_page = Number(i);
                                            }

                                            $("#div_content_bg").show();//开始禁止模式 
                                            isfuduing = true;
                                            reread(fudu_content[0],pindex,pages.length);  
                                            // $(fudu_content[1]).css('border','1px solid red'); 
                                            var mySwiper = document.querySelector('.swiper-container').swiper;
                                            mySwiper.slideTo((Number($(fudu_content[0]).attr("curpage"))),1000);                                        

                                        }
                                        
                                        return;
                                    }


                                    read(this,pindex,pages.length);

                                };


                                //单独div对象添加到 集合中
                                if(divs.indexOf($("#"+cid)) == -1){
                                    divs[j] = $("#"+cid);
                                }

                            }

                            if(page_divs.indexOf(divs) == -1){
                                page_divs[pindex] = divs;
                            }

                            return;

                        }
                        

                    }

                }
            }


        }





//初始化audio
function initAudio(){

    // audioHandler = $$("#music_player")[0];
    audioHandler = new AudioPlayer({
     id: 'music_player'
 });

    
    audioHandler.on('canplay', function (totalTime, name, item) {

        // console.log(audioHandler.audio.playbackRate);
        audioHandler.play();
        $("#bottom_menu_img2").attr("src","../img/icon_unplay.png");
        isreadingover = false;
        console.log('canplay');
        curalltime = totalTime;
    });

    
    audioHandler.on('ended' , function (){
       
            console.log("audioHandler ======== ended");
            audioEndFunc();

    });



    audioHandler.on('play' , function (){
        console.log('play');
        
        console.log(audioHandler.audio.playbackRate);

        
});

}



//播放完成执行的函数
function audioEndFunc(){

var record_curalltime = Math.ceil(curalltime*1.2);

            // curtime = setTimeout(function(){
                isreadingover = true;
               if(playstate == 0){
                   $("#bottom_menu_img2").attr("src","../img/icon_play.png");
                   $("#div_tips").hide();


                if(isfollow){//如果是录音模式，开始录制声音
                    //开始录制声音

                    if(record_playnum == 1){//第一次
                        beginFollow(record_curalltime);
                        return;
                    }else if(record_playnum == 2){//第二次，开始播放本地录音
                        record_playnum = 1;
                        playRecord();
                        return;
                    }
                    
                }


                //  if(ispause){

                // }else{
                //     audioHandler.pause();
                //     read(curobject,curpindex,curpagenum);
                // }

             }else if(playstate == 1){//全局阅读，本课

               if(strs[1]>=curpagenum || ispause){
                audioHandler.pause();
                $("#bottom_menu_img2").attr("src","../img/icon_play.png");
                $("#div_tips").hide();

                }else{

                    var ccid = strs[0]+"_"+(Number(strs[1])+1);
                    // //先判断是否是重复的音频，如果是重复的音频要跳过重复音频阅读
                    // if(Number($("#"+ccid).attr("duplicate")) == 1){// == 1 为重复音频
                    //     ccid = strs[0]+"_"+(Number(strs[1])+2);
                    //     if(((Number(strs[1])+2) >= curpagenum){

                    //     }

                    // };
                        if(isfollow){//如果是录音模式，开始录制声音
                        //开始录制声音

                            if(record_playnum == 1){//第一次
                                beginFollow(record_curalltime);
                                return;
                            }else if(recurpage == 2){//第二次，开始播放本地录音
                                playRecord();

                                setTimeout(function(){

                                    //跟读模式
                                    read($("#"+ccid),curpindex,curpagenum);  

                                },(record_curalltime+1));


                                return;
                            }

                        }

                        //正常阅读
                        read($("#"+ccid),curpindex,curpagenum);    
                }

             }else if(playstate == 2){//循环阅读，圈选复读
                // alert($(curobject).attr("cid")+"=="+$(fudu_content[1]).attr("cid")+"当前页词语数量:"+curpagenum);
                // strs = ($(curobject).attr("id")).split("_");

                if($(curobject).attr("cid") == $(fudu_content[1]).attr("cid")){//读完了,从头开始复读
                    // dismissFuDu();

                    isfuduing = true;
                    var fudu_pindex = $(fudu_content[0]).attr("curpage");
                    var fudu_pagenum = $(fudu_content[0]).attr("pagenum");
                    var mySwiper = document.querySelector('.swiper-container').swiper;
                    mySwiper.slideTo((Number(fudu_pindex)),1000);     


                    if(isfollow){//如果是录音模式，开始录制声音
                    //开始录制声音

                        if(record_playnum == 1){//第一次
                            beginFollow(record_curalltime);
                            return;
                        }else if(recurpage == 2){//第二次，开始播放本地录音
                            playRecord();

                            setTimeout(function(){

                                //跟读模式
                                reread(fudu_content[0],fudu_pindex,fudu_pagenum); 

                            },(record_curalltime+1));


                            return;
                        }

                    }



                    reread(fudu_content[0],fudu_pindex,fudu_pagenum);  
                    // $(fudu_content[1]).css('border','1px solid red'); 
                }else{//没有读完，接着读

                    if(ispause){//暂停
                       dismissRead();
                       $("#div_content_bg").hide();
                       return;
                   }
                // alert("str[1]:"+strs[1]+"--"+"curpagenum:"+curpagenum+"==");
                    if(Number(strs[1]) >= Number(curpagenum)){//本页面读取完了

                        var ccid = (Number(strs[0])+1)+"_"+1;
                        var mySwiper = document.querySelector('.swiper-container').swiper;
                        // alert(ccid);
                        // alert("nextccid:"+Number($("#"+ccid).attr("curpage")));
                        mySwiper.slideTo(Number($("#"+ccid).attr("curpage")),1000);           
                        // alert(ccid);
                    }else{//本页面没有读完
                        var ccid = strs[0]+"_"+(Number(strs[1])+1);
                    }



                    if(isfollow){//如果是录音模式，开始录制声音
                    //开始录制声音

                        if(record_playnum == 1){//第一次
                            beginFollow(record_curalltime);
                            return;
                        }else if(recurpage == 2){//第二次，开始播放本地录音
                            playRecord();

                            setTimeout(function(){

                                //跟读模式
                                reread($("#"+ccid),curpindex,curpagenum); 

                            },(record_curalltime+1));


                            return;
                        }

                    }

                    reread($("#"+ccid),curpindex,curpagenum); 
                }
                
             }else if(playstate == 3){//连续
                    if(ispause){//暂停
                       dismissRead();
                       return;
                   }
                // alert("str[1]:"+strs[1]+"--"+"curpagenum:"+curpagenum+"==");
                    if(Number(strs[1]) >= Number(curpagenum)){//本页面读取完了
                        var ccid = (Number(strs[0])+1)+"_"+1;
                        var mySwiper = document.querySelector('.swiper-container').swiper;
                        

                        if(isNotNull($("#"+ccid).attr("curpage"))){

                            mySwiper.slideTo(Number($("#"+ccid).attr("curpage")),1000);               
                            
                        }else{
                            var nextpageindex = Number(curpindex)+Number(nextpageindex_add);
                            // alert(nextpageindex);
                            createCheckImage(nextpageindex);
                            // alert($("#"+ccid).attr("curpage"));
                            if(isNotNull($("#"+ccid).attr("curpage"))){
                                mySwiper.slideTo(Number($("#"+ccid).attr("curpage")),1000);            
                                nextpageindex_add = nextpageindex_add +1;
                            }else{//读完了全部
                                dismissRead();
                                return;
                            }
                        }
                        // alert("nextccid:"+Number($("#"+ccid).attr("curpage")));
                        // alert(ccid);
                    }else{//本页面没有读完
                        ccid = strs[0]+"_"+(Number(strs[1])+1);
                    }
                    var curpagelength = $("#"+ccid).attr("pagenum");







                    if(isfollow){//如果是录音模式，开始录制声音
                    //开始录制声音

                        if(record_playnum == 1){//第一次
                            beginFollow(record_curalltime);
                            return;
                        }else if(recurpage == 2){//第二次，开始播放本地录音
                            playRecord();

                            setTimeout(function(){

                                //跟读模式
                                read($("#"+ccid),curpindex,curpagelength);    

                            },(record_curalltime+1));


                            return;
                        }

                    }


                    read($("#"+ccid),curpindex,curpagelength);    

             }else if(playstate == 4){//查词


             }else if(playstate == 5){//连续阅读单句
               if(ispause){

               }else{

                audioHandler.pause();

                    if(isfollow){//如果是录音模式，开始录制声音
                    //开始录制声音

                        if(record_playnum == 1){//第一次
                            beginFollow(record_curalltime);
                            return;
                        }else if(recurpage == 2){//第二次，开始播放本地录音
                            playRecord();

                            setTimeout(function(){

                                //跟读模式
                                read(curobject,curpindex,curpagenum);    

                            },(record_curalltime+1));


                            return;
                        }

                    }



                    
                    read(curobject,curpindex,curpagenum);
                }
            }

        // },((curalltime*1200)/cur_play_sudu))







}



//停止复读模式，重新开始阅读起点和终点
function click_fududiv_bg(){
    dismissRead();
    dismissFuDu();
    clearBorder();
    playstate == 2;
    createCheckImage(pageindex);
    $.toast("已停止复读，请重新选择阅读起点和终点");
    
}


//开始重复阅读
function reread(ob,pindex,pagenum){

    //全局
    curpagenum = $(ob).attr("pagenum");
    curpindex = pindex;
    curobject = ob;
    

    clearBorder();
    var recurpage = Number($(ob).attr("curpage"));
    if(end_du_page == recurpage){
        $(fudu_content[1]).css('border','1px solid red');
    }
    $(ob).css('border','1px solid #0665A5');
    var strid = $(ob).attr("id")+"";
    var str_tips_arr = $(ob).attr("title")+"";
    var tips_arr = str_tips_arr.split("0_0!!");
    var tips = tips_arr[0];
    console.log('id:'+strid);
    strs = strid.split("_");
    var msrc = "https://gitee.com/huaflower/ReadBook_CommonFile/raw/master/sound/"+(strs[0])+"_"+(strs[1])+".mp3";
    // var msrc = "../sound/"+(strs[0])+"_"+(strs[1])+".mp3";
     // $("#bgmusic").get(0).pause();
     $("#bottom_menu_img2").attr("src","../img/icon_unplay.png");

     $("#div_tips").hide();
     $("#div_tips").html(tips);
     if(istranslation){
        msrc = "https://gitee.com/huaflower/ReadBook_CommonFile/raw/master/sound/"+(strs[0])+"_"+(strs[1])+"zh.mp3";

        // msrc = "../sound/"+(strs[0])+"_"+(strs[1])+"zh.mp3";
        if(isNotNull(tips)){
            $("#div_tips").show();       
        }
        
    }

    var list = [{
        name: strid,
        url: msrc,
        time: $(ob).attr("duration")
    }];

    //播放声音
    // $("#bgmusic").html("<source src="+msrc+">");
    // $("#bgmusic").get(0).load();

    ispause = 0;
    clearTimeout(curtime);


// //先判断是否是重复的音频，如果是重复的音频要跳过重复音频阅读  ,并且播放状态为 1，2，3 时   ， 并且 必须为非点击播放才会自动下一句
    if(Number($(ob).attr("duplicate")) == 1 && (playstate == 1 || playstate == 2 || playstate == 3) && !checkplay){// == 1 为重复音频
        audioEndFunc();//重新阅读下句
        checkplay = false;
        return;
    };
    checkplay = false;


    audioHandler.clearList();
    audioHandler.add(list);
    audioHandler.loadIndex(0);

    if(cur_play_sudu > 1){//如果当前的速度大于1，ios 机型大于1 的速度回播放不出声音，用本地的播放器来代替播放
        var param = {
            type:'play_local_sounds',
            speed:cur_play_sudu,
            soundsrc:msrc
        };
        //开始去录音
        send_data(param);
    }
}



//设置当前页面的z-index最大
function setCurZIndexTop(){
     
        for(var k = 0;k<page_divs.length;k++){
            var divs = page_divs[k];       
            if(isNotNull(divs)){
                for(var i = 0;i<divs.length;i++){
                    var cdiv = divs[i];
                    if(isNotNull(cdiv)){
                        if(k == pageindex){//当前页面z-index最大
                            console.log("当前页面的优先级最高："+k+"=====");
                            $(cdiv).attr("z-index",110);
                        }else{
                            $(cdiv).attr("z-index",100);
                        }
                        
                    }
                }
            }
        }
}


//清空边框
function clearBorder(){
    if(!isshowchecked){
        $("#div_container div").css('border','0px solid white');
    }else{
        var divs = page_divs[pageindex];
        $("#div_container div").css('border','0px solid white');
        if(isNotNull(divs)){

            for(var i = 0;i<divs.length;i++){
                var cdiv = divs[i];
                console.log("当前页面的id："+cdiv.attr("id"));

                if(isNotNull(cdiv)){
                    if(i < Number(curpagenum) || curpagenum == 0){
                        $(cdiv).css('border','1px solid #bababa');
                    }
                    
                }
            }
        }
        
        // $("#div_container div").css('border','1px solid #bababa');
    }
}

//开始阅读
function read(ob,pindex,pagenum){

    console.log("read ========== read"+$(ob).attr("id"));

    //全局
    curpagenum = pagenum;
    curpindex = pindex;
    curobject = ob;

    clearBorder();
    $(ob).css('border','1px solid #0665A5');
    var strid = $(ob).attr("id")+"";
    var str_tips_arr = $(ob).attr("title")+"";
    var tips_arr = str_tips_arr.split("0_0!!");
    var tips = tips_arr[0];
    console.log('id:'+strid);
    strs = strid.split("_");
    var msrc = "https://gitee.com/huaflower/ReadBook_CommonFile/raw/master/sound/"+(strs[0])+"_"+(strs[1])+".mp3";
    // var msrc = "../sound/"+(strs[0])+"_"+(strs[1])+".mp3";
     // $("#bgmusic").get(0).pause();
     console.log(msrc);
     $("#bottom_menu_img2").attr("src","../img/icon_unplay.png");
     $("#div_tips").hide();
     $("#div_tips").html(tips);
     if(istranslation){
        msrc = "https://gitee.com/huaflower/ReadBook_CommonFile/raw/master/sound/"+(strs[0])+"_"+(strs[1])+"zh.mp3";
        // msrc = "../sound/"+(strs[0])+"_"+(strs[1])+"zh.mp3";
        if(isNotNull(tips)){
            if(tips.length >= 150){
                $("#div_tips").css("font-size",15);
            }
            $("#div_tips").show();   
        }
        
    }

    if(!isNotNull(msrc)){
        //无声音乐

        msrc = 'https://gitee.com/huaflower/ReadBook_CommonFile/raw/master/nosound/nosound.mp3';
    }
    
    var list = [{
        name: strid,
        url: msrc,
        time: $(ob).attr("duration")
    }];

    //播放声音
    // $("#bgmusic").html("<source src="+msrc+">");
    // $("#bgmusic").get(0).load();

    ispause = 0;
    clearTimeout(curtime);


// //先判断是否是重复的音频，如果是重复的音频要跳过重复音频阅读  ,并且播放状态为 1，2，3 时   ， 并且 必须为非点击播放才会自动下一句
    if(Number($(ob).attr("duplicate")) == 1 && (playstate == 1 || playstate == 2 || playstate == 3) && !checkplay){// == 1 为重复音频
        audioEndFunc();//重新阅读下句
        checkplay = false;
        return;
    };
    checkplay = false;


    audioHandler.clearList();
    audioHandler.add(list);
    audioHandler.loadIndex(0);
    
  /* $("#bgmusic").get(0).oncanplay = function () {  
           // sounds = $("#bgmusic").get(0).duration;     
           $("#bgmusic").get(0).play();
           setTimeout(function(){

            if(playstate == 0){
                $("#bottom_menu_img2").attr("src","../img/icon_play.png");

            }else if(playstate == 1){//全局阅读

                if(strs[1]>=pagenum){
                    $("#bottom_menu_img2").attr("src","../img/icon_play.png");
                }else{

                    var ccid = strs[0]+"_"+(Number(strs[1])+1);
                    read($("#"+ccid),pindex,pagenum);    
                }
                

            }else if(playstate == 2){//循环阅读

                if($("#bgmusic").get(0).paused){

                }else{
                    read(ob,pindex,pagenum);
                }
                

            }
             
           },sounds*1000)
       }*/



     if(cur_play_sudu > 1){//如果当前的速度大于1，ios 机型大于1 的速度回播放不出声音，用本地的播放器来代替播放
       var param = {
           type:'play_local_sounds',
           speed:cur_play_sudu,
           soundsrc:msrc
       };
         //开始去录音
         send_data(param);
     }

 }


 function showmenu(){
    $("#div_words_bg").show();
    toshare();
}


//是否显示可点击读框
function showChecked(){
    if(!isshowchecked){
        $.toast("显示点读框");
        $("#img_showchecked").attr("src","../img/img_hand_check.png");
        isshowchecked = true;
        clearBorder();

    }else{
        $.toast("隐藏点读框");
        $("#img_showchecked").attr("src","../img/img_hand_uncheck.png");
        isshowchecked = false;
        clearBorder();
    }
}


//点击改变播放音频的状态
function readstate(){

   /* if($("#bgmusic").get(0).paused){//暂停
        $("#bgmusic").get(0).play();
        
        $("#bottom_menu_img2").attr("src","../img/icon_unplay.png");
    }else{
        $("#bgmusic").get(0).pause();
        $("#bottom_menu_img2").attr("src","../img/icon_play.png");
    }*/

    if(audioHandler.playing){//暂停
        ispause = 1;
        audioHandler.pause();
        $("#bottom_menu_img2").attr("src","../img/icon_play.png");

        // if(isfuduing){
        //     dismissFuDu();
        // }

    }else{
        ispause = 0;
        
        if(isNotNull(audioHandler.list)){//如果为空，默认从头开始播放

            var curpro_play = audioHandler.audio.currentTime;
            console.log("当前的播放进度是："+audioHandler.audio.currentTime);
            if(isNotNull(curpro_play) && !isNaN(curpro_play)){
                curpro_play = curpro_play - 1;
                if(curpro_play < 0){
                    curpro_play = 0;
                }
                if(!isreadingover){
                    audioHandler.audio.currentTime = curpro_play;
                }
                
            }
            audioHandler.play();
        }else{

            var cdiv = page_divs[pageindex][0];
            
            read(cdiv,pageindex,position_data[pageindex].page_data.length);
            
            
        }
        
        $("#bottom_menu_img2").attr("src","../img/icon_unplay.png");
        
    }
}


//弹出层
function toshare(){
    $("#div_content_bg").hide();//关闭禁止模式 

    $(".am-share").addClass("am-modal-active"); 
    if($(".sharebg").length>0){
        $(".sharebg").addClass("sharebg-active");
    }else{
        $("body").append('<div class="sharebg"></div>');
        $(".sharebg").addClass("sharebg-active");
    }
    $(".sharebg-active,.share_btn").click(function(){
        $(".am-share").removeClass("am-modal-active");  
        setTimeout(function(){
            $(".sharebg-active").removeClass("sharebg-active"); 
            $(".sharebg").remove(); 
        },300);
    })

} 

function menu1(){
    $("#div_words_bg").hide();
    $("#li_menu4").css("background-image","url(../img/icon_menu1_off.png)");
    $("#li_menu5").css("background-image","url(../img/icon_menu2_off.png)");
    $("#li_menu6").css("background-image","url(../img/icon_menu3_off.png)");
    $("#li_menu7").css("background-image","url(../img/icon_menu4_off.png)");
    $("#li_menu8").css("background-image","url(../img/icon_menu5_off.png)");
    $("#li_menu9").css("background-image","url(../img/icon_menu6_off.png)");

    $("#li_menu4").css("background-image","url(../img/icon_menu1_on.png)");
    $(".am-share").removeClass("am-modal-active");  
    setTimeout(function(){
        $(".sharebg-active").removeClass("sharebg-active"); 
        $(".sharebg").remove(); 
    },300);
    playstate = 0;

    if(isfuduing){//判断是否正在复读
        dismissFuDu();
    }
    dismissRead();
}  

function menu2(){
    $("#div_words_bg").hide();
    $("#li_menu4").css("background-image","url(../img/icon_menu1_off.png)");
    $("#li_menu5").css("background-image","url(../img/icon_menu2_off.png)");
    $("#li_menu6").css("background-image","url(../img/icon_menu3_off.png)");
    $("#li_menu7").css("background-image","url(../img/icon_menu4_off.png)");
    $("#li_menu8").css("background-image","url(../img/icon_menu5_off.png)");
    $("#li_menu9").css("background-image","url(../img/icon_menu6_off.png)");

    $("#li_menu5").css("background-image","url(../img/icon_menu2_on.png)");
    $(".am-share").removeClass("am-modal-active");  
    setTimeout(function(){
        $(".sharebg-active").removeClass("sharebg-active"); 
        $(".sharebg").remove(); 
    },300);

    playstate = 1;

            if(isfuduing){//判断是否正在复读
                dismissFuDu();
            }
            dismissRead();
        } 

        function menu3(){
            $("#div_words_bg").hide();
            $("#li_menu4").css("background-image","url(../img/icon_menu1_off.png)");
            $("#li_menu5").css("background-image","url(../img/icon_menu2_off.png)");
            $("#li_menu6").css("background-image","url(../img/icon_menu3_off.png)");
            $("#li_menu7").css("background-image","url(../img/icon_menu4_off.png)");
            $("#li_menu8").css("background-image","url(../img/icon_menu5_off.png)");
            $("#li_menu9").css("background-image","url(../img/icon_menu6_off.png)");

            $("#li_menu6").css("background-image","url(../img/icon_menu3_on.png)");
            $(".am-share").removeClass("am-modal-active");  
            setTimeout(function(){
                $(".sharebg-active").removeClass("sharebg-active"); 
                $(".sharebg").remove(); 
            },300);

            // playstate = 5;
            playstate = 2;
            clearBorder();
            
            fudu_content = new Array();

            if(isfuduing){//判断是否正在复读
                dismissFuDu();
            }
            dismissRead();
        } 


        function menu4(){
            $("#div_words_bg").hide();
            $("#li_menu4").css("background-image","url(../img/icon_menu1_off.png)");
            $("#li_menu5").css("background-image","url(../img/icon_menu2_off.png)");
            $("#li_menu6").css("background-image","url(../img/icon_menu3_off.png)");
            $("#li_menu7").css("background-image","url(../img/icon_menu4_off.png)");
            $("#li_menu8").css("background-image","url(../img/icon_menu5_off.png)");
            $("#li_menu9").css("background-image","url(../img/icon_menu6_off.png)");

            $("#li_menu7").css("background-image","url(../img/icon_menu4_on.png)");
            $(".am-share").removeClass("am-modal-active");  
            setTimeout(function(){
                $(".sharebg-active").removeClass("sharebg-active"); 
                $(".sharebg").remove(); 
            },300);

            playstate = 3;

            if(isfuduing){//判断是否正在复读
                dismissFuDu();
            }
            dismissRead();
        }

        function menu5(){
            $("#div_words_bg").hide();
            $("#li_menu4").css("background-image","url(../img/icon_menu1_off.png)");
            $("#li_menu5").css("background-image","url(../img/icon_menu2_off.png)");
            $("#li_menu6").css("background-image","url(../img/icon_menu3_off.png)");
            $("#li_menu7").css("background-image","url(../img/icon_menu4_off.png)");
            $("#li_menu8").css("background-image","url(../img/icon_menu5_off.png)");
            $("#li_menu9").css("background-image","url(../img/icon_menu6_off.png)");

            $("#li_menu8").css("background-image","url(../img/icon_menu5_on.png)");

            $(".am-share").removeClass("am-modal-active");  
            setTimeout(function(){
                $(".sharebg-active").removeClass("sharebg-active"); 
                $(".sharebg").remove(); 
            },300);

            playstate = 4;

            if(isfuduing){//判断是否正在复读
                dismissFuDu();
            }
            dismissRead();
        }


        function menu6(){
            $("#div_words_bg").hide();
    // $("#li_menu4").css("background-image","url(../img/icon_menu1_off.png)");
    // $("#li_menu5").css("background-image","url(../img/icon_menu2_off.png)");
    // $("#li_menu6").css("background-image","url(../img/icon_menu3_off.png)");
    // $("#li_menu7").css("background-image","url(../img/icon_menu4_off.png)");
    // $("#li_menu8").css("background-image","url(../img/icon_menu5_off.png)");
    // $("#li_menu9").css("background-image","url(../img/icon_menu6_off.png)");

    // $("#li_menu9").css("background-image","url(../img/icon_menu6_on.png)");

    $(".am-share").removeClass("am-modal-active");  
    setTimeout(function(){
        $(".sharebg-active").removeClass("sharebg-active"); 
        $(".sharebg").remove(); 
    },300);

            // playstate = 0;
            showChangeSudu();
            if(isfuduing){//判断是否正在复读
                dismissFuDu();
            }
            // dismissRead();
        }


//开始点击查词,弹出框框
function searchWords(ob){

    var str_tips_arr = $(ob).attr("title")+"";
    var tips_arr = str_tips_arr.split("0_0!!");
    var text = tips_arr[1];
    curtrans = tips_arr[0];
    $("#div_words_dialog_content").html(text);
    $("#div_words_dialog").show();
    $("#div_words_bg").show();
}

//关闭弹窗
function closeDialog(){
    $("#div_words_dialog").hide();
    $("#div_words_bg").hide();
}

//获取选中的文本，执行查词功能
function getCurText(){

    var text = window.getSelection().toString();
    //转换成小写
    text = text.toLowerCase();

    if((Object.keys(dicdata)).indexOf(text) == -1){
        //开始弹出查词的框
        // alert("查词解释：  暂无此解释");
//         $.alert('暂无此解释', '查词解释');
native_showdialog("暂无此解释");
}else{
        //开始弹出查词的框
//        $.alert(" "+text+": "+dicdata[text], '查词解释');
        // alert("查词解释：  "+text+": "+dicdata[text]);
        native_showdialog(" "+text+": "+dicdata[text]);
    }
    
}

//停止阅读，比如切换菜单类型跟暂停时
function dismissRead(){
    //执行默认
    ispause = 1;
    audioHandler.pause();
    $("#bottom_menu_img2").attr("src","../img/icon_play.png");
    $("#div_tips").hide();
}

//停止复读
function dismissFuDu(){
    isfuduing = false;
    audioHandler.pause();
    $("#bottom_menu_img2").attr("src","../img/icon_play.png");
    $("#div_tips").hide();
    $("#div_content_bg").hide();
    clearBorder();
    // $("#div_container div").css('border','0px solid white');
    fudu_content = new Array();
}

//显示目录菜单
function showRightMenu(){

    $("#div_right").show();
    $("#div_words_bg").show();
    $("#div_words_dialog").hide();
    $(".am-share").removeClass("am-modal-active");  
    setTimeout(function(){
        $(".sharebg-active").removeClass("sharebg-active"); 
        $(".sharebg").remove(); 
    },300);
}

//关闭目录菜单
function dismissRightMenu(){

    $("#div_right").hide();
    $("#div_words_bg").hide();  

}


//弹出 速度设置层
function showChangeSudu(){
    $("#div_chang_sudu").show();
    $("#div_words_bg").show();
}

//隐藏速度层
function hideChangeSudu(){
    $("#div_chang_sudu").hide();
    // var audioElm = document.getElementById("music_player"); // Audio element
    var sudu = $$(".single-slider").val();   
    if(sudu == 6){//中
        audioHandler.audio.defaultPlaybackRate = 1;
        audioHandler.audio.playbackRate = 1;
        cur_play_sudu = 1;
    }else if(sudu > 6){//快
        var csd = (sudu - 6)*0.1;
        audioHandler.audio.defaultPlaybackRate = (1+csd);
        audioHandler.audio.playbackRate = (1+csd);
        cur_play_sudu = (1+csd);
    }else if(sudu < 6){//慢
        var csd = (6-sudu)*0.1;
        audioHandler.audio.defaultPlaybackRate = (1-csd);
        audioHandler.audio.playbackRate = (1-csd);
        cur_play_sudu = (1-csd);
    }
    console.log("当前的播放速度："+audioHandler.audio.playbackRate);
    // alert(audioElm.playbackRate);
}



//跟读，开启跟读模式
function follow(){

    if(isfollow){
        $.toast("已关闭跟读模式");
        $("#bottom_menu_img4").attr('src', '../img/img_icon_replay_off.png'); 
        isfollow = false;
    }else{
        $.toast("已开启跟读模式");
        $("#bottom_menu_img4").attr('src', '../img/img_icon_replay_on.png'); 
        isfollow = true;
        record_playnum = 1;
    }

}


//去播放跟读模式下，开始录音然后播放
function beginFollow(ptime){
    $.toast("开始录音");
    var param = {
        type:'begin_record',
        time:ptime
    };
    //开始去录音
    send_data(param);

}


//结束录音了，开始播放刚才的课本然后再次播放录音
function stopRecord(){
    //录音结束，再次播放上一次的声音，调用全局的一个播放
    if(ispause){          
    }else{
        record_playnum = 2;
        $.toast("开始播放");
        audioHandler.pause();
        read(curobject,curpindex,curpagenum);
    }
}

//结束课本文件后，开始去播放本地的录音，结束后，next
function playRecord(){

    var param = {
        type:'play_record'
    };
    //开始去录音
    send_data(param);
}



// http://dict-co.iciba.com/api/dictionary.php?w=go&key=DE5EB7C205E95AB2884CB14102E64369&type=json
//是否需要翻译提示
function translation_change(){
    if(istranslation){
        $.toast("已关闭翻译");
        $("#bottom_menu_img3").attr('src', '../img/img_translation_off.png'); 
        istranslation = false;
    }else{
        $.toast("已开启翻译");
        $("#bottom_menu_img3").attr('src', '../img/img_translation_on.png'); 
        istranslation = true;
    }
}


//添加子元素
function addDivChildByNotExist(fatherdiv,strchild,cid){

    if(isNotNull(fatherdiv)){
        if($("#"+cid).length > 0){
            console.log("remove")
            $("#"+cid).remove();
        }
        fatherdiv.appendChild(strchild);
    }
}

//比较id的大小，前者大返回 1 ，后者大返回-1，相同返回 0
function compareId(a,b){
    var s_id_a = (a.split("_"))[0];
    var s_id_b = (a.split("_"))[1];
    var e_id_a = (b.split("_"))[0];
    var e_id_b = (b.split("_"))[1];

    if(a == b){
        return 0;
    }

    if(Number(s_id_a) > Number(e_id_a)){
        return 1;
    }

    if((Number(s_id_a) == Number(e_id_a)) && 
        (Number(s_id_b) > Number(e_id_b)) ){
        return 1;
}

return -1;

}

